package com.hxd.otc.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.utils.DataUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2018/6/8 0008.
 * description ${TODO}
 */
public class EditInfoInputView extends LinearLayout {

    @BindView(R.id.tv_label)
    TextView mTvLabel;
    @BindView(R.id.et_input)
    TextView mEtInput;

    public EditInfoInputView(Context context) {
        this(context, null);
    }

    public EditInfoInputView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EditInfoInputView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EditInfoInputView, defStyleAttr, 0);
        CharSequence labelText = a.getText(R.styleable.EditInfoInputView_labelText);
        CharSequence inputText = a.getText(R.styleable.EditInfoInputView_inputText);
        CharSequence inputHint = a.getText(R.styleable.EditInfoInputView_inputHint);
        float labelTextSize = a.getDimension(R.styleable.EditInfoInputView_labelTextSize, 14);
        float inputTextSize = a.getDimension(R.styleable.EditInfoInputView_inputTextSize, 14);
        float paddingRight = a.getDimension(R.styleable.EditInfoInputView_inputPaddingRight, DataUtils.dp2px(getContext(), 10));
        int labelTextColor = a.getColor(R.styleable.EditInfoInputView_labelTextColor, getResources().getColor(R.color.color_EditText_text));
        int inputTextColor = a.getColor(R.styleable.EditInfoInputView_inputTextColor, getResources().getColor(R.color.color_EditText_text));
        int inputHintColor = a.getColor(R.styleable.EditInfoInputView_inputHintColor, getResources().getColor(R.color.color_EditText_textHint));
        boolean isSingleLine = a.getBoolean(R.styleable.EditInfoInputView_singleLine, true);
        int maxLines = a.getInt(R.styleable.EditInfoInputView_maxLines, 1);
        int maxLength = a.getInt(R.styleable.EditInfoInputView_maxLength, -1);
        a.recycle();

        setOrientation(HORIZONTAL);
        LayoutInflater.from(context).inflate(R.layout.view_edit_info_input, this);
        setBackgroundColor(getResources().getColor(android.R.color.white));
        ButterKnife.bind(this);

        setLabelText(labelText);
        setLabelTextColor(labelTextColor);
        setLabelTextSize(labelTextSize);

        setInputText(inputText);
        setInputHint(inputHint);
        setInputTextSize(inputTextSize);
        setInputPaddingRight((int) paddingRight);
        setInputTextColor(inputTextColor);
        setHintTextColor(inputHintColor);
        setLines(maxLines, isSingleLine);
        setMaxLength(maxLength);
    }

    public void setHintTextColor(int inputHintColor) {
        mEtInput.setHintTextColor(inputHintColor);
    }

    public void setInputTextColor(int inputTextColor) {
        mEtInput.setTextColor(inputTextColor);
    }

    public void setInputTextSize(float inputTextSize) {
        mEtInput.setTextSize(inputTextSize);
    }

    private void setInputPaddingRight(int paddingRight) {
        mEtInput.setPadding(mEtInput.getPaddingLeft(), mEtInput.getPaddingTop(), paddingRight, mEtInput.getPaddingBottom());
    }

    public void setInputHint(CharSequence inputHint) {
        mEtInput.setHint(inputHint);
    }

    public String getHint() {
        return mEtInput.getHint().toString().trim();
    }

    public void setInputText(CharSequence inputText) {
        mEtInput.setText(inputText);
    }

    public void setInputTextEnable(boolean enable) {
        mEtInput.setEnabled(enable);
    }

    public String getText() {
        return mEtInput.getText().toString().trim();
    }

    public void setLabelTextSize(float labelTextSize) {
        mTvLabel.setTextSize(labelTextSize);
    }

    public void setLabelTextColor(int labelTextColor) {
        mTvLabel.setTextColor(labelTextColor);
    }

    public void setLabelText(CharSequence labelText) {
        mTvLabel.setText(labelText);
    }

    public void setLines(int maxLines, boolean isSingleLine) {
        mEtInput.setMaxLines(maxLines);
        mEtInput.setSingleLine(isSingleLine);
    }

    public void setMaxLength(int maxLength) {
        if (maxLength == -1) return;
        mEtInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
    }

    public void setInputType(int type) {
        mEtInput.setInputType(type);
    }

    public void setFilters(InputFilter[] filters) {
        mEtInput.setFilters(filters);
    }
}
